package com.link.base.base.position.service;

import com.link.base.base.dept.dao.mybatis.mapper.DepartmentMapper;
import com.link.base.base.dept.model.Department;
import com.link.base.base.position.dao.mybatis.mapper.PositionImpMapper;
import com.link.base.base.position.dao.mybatis.mapper.PositionMapper;
import com.link.base.base.position.model.Position;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.model.BasicModel;
import com.link.core.basic.model.CallParam;
import com.link.core.basic.query.QueryParams;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.ServiceException;
import com.link.core.cllog.LCLogger;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author unknown
 * @date unknown
 * @comments 职位操作服务
 */
@Service
public class PositionImpServiceImpl extends BasicServiceImpl<Position> implements PositionImpService {

    @Resource
    private PositionImpMapper positionImpMapper;

    @Resource
    private PositionMapper positionMapper;

    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public BasicMapper<Position> getBasicMapper() {
        return positionImpMapper;
    }

    @Override
    public void insert(Position record) throws ServiceException {
        QueryParams qps = new QueryParams();
        try {
            //如果父职位没有指定，则添加默认父职位
            if (StringUtils.isNull(record.getParentPositionName())) {
                //qps.setAttr1(AppConstants.IS_EFFECTIVE);
                //qps.setAttr2(AppConstants.POSTN_TYPE);
                Position t = (Position) BasicModel.transformClass(Position.class, qps);
                //获取根职位的名称
                String parentPositionName = positionMapper.queryRoot(t).getText();
                record.setParentPositionName(parentPositionName);
            }

            if ("".equals(record.getDeptName()) || record.getDeptName() == null) {
                //qps.setAttr1(AppConstants.IS_EFFECTIVE);
                //qps.setAttr2(AppConstants.DEPT_TYPE);
                Department t = (Department) BasicModel.transformClass(Department.class, qps);
                //获取根部门的名称
                String parentDeptName = departmentMapper.queryRoot(t).getText();
                //添加默认部门
                record.setDeptName(parentDeptName);
            }
            record.setId(keyGenerateService.keyGenerate());
            positionImpMapper.insert(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException("POSITION-001");
        }
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void runPositionCheck(CallParam record) throws ServiceException {
        try {
            CoreUser user = UserUtil.getUser();
            record.setParam1(user.getId());
            positionImpMapper.runPositionCheck(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException("POSITION-002");
        }
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void runNewPositionForimp(CallParam record) throws ServiceException {
        try {
            CoreUser user = UserUtil.getUser();
            record.setParam1(user.getId());
            positionImpMapper.runPositionCheck(record);
            positionImpMapper.runNewPositionForimp(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException("POSITION-002");
        }
    }
}
